package com.liuyang.concurrency.chapter7;

public class Synchronized2Runnable implements Runnable{
    private int index = 1;

    private int MAX = 500;

    private final Object MONITOR = new Object();
    /**
     * When an object implementing interface <code>Runnable</code> is used
     * to create a thread, starting the thread causes the object's
     * <code>run</code> method to be called in that separately executing
     * thread.
     * <p>
     * The general contract of the method <code>run</code> is that it may
     * take any action whatsoever.
     *
     * @see Thread#run()
     */
    @Override
    public void run() {
        while (true){
            if (ticket())
                break;
        }
    }

    private synchronized boolean ticket() {
        if(index > MAX){
            return true;
        }
        try {
            Thread.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(Thread.currentThread() + " 的号码是："+ (index++));
        return false;
    }
}
